<?php

class autoload implements IService{

    private $_handler = null;
    private $_classMap = array();
    
    public function __construct($driver){
        if(is_null($this->_handler)){
            $this->_handler = driverFactory($driver);
            $driverPath = dirname(__FILE__).DS.'driver'.DS.$driver.'.php';
            if(is_file($driverPath)) include($driverPath);
        }
    }

    public function load($className){
        if(isset($this->_classMap[$className]))
            include($this->_classMap[$className]);
        else
        {
            if(strpos($className,'\\')===false)  // class without namespace
            {
                if(self::$enableIncludePath===false)
                {
                    foreach(self::$_includePaths as $path)
                    {
                        $classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
                        if(is_file($classFile))
                        {
                            include($classFile);
                            if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
                                throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
                                    '{class}'=>$className,
                                    '{file}'=>$classFile,
                                )));
                            break;
                        }
                    }
                }
                else
                    include($className.'.php');
            }
            else
            {
                $namespace=str_replace('\\','.',ltrim($className,'\\'));
                if(($path=self::getPathOfAlias($namespace))!==false)
                    include($path.'.php');
                else
                    return false;
            }
            return class_exists($className,false) || interface_exists($className,false);
        }
        return true;
    }

}